package com.tju.ocean_ecology_website.controller;

import com.tju.ocean_ecology_website.dto.ApiResponse;
import com.tju.ocean_ecology_website.dto.PageResult;
import com.tju.ocean_ecology_website.dto.StatusUpdateDTO;
import com.tju.ocean_ecology_website.dto.UserQueryDTO;
import com.tju.ocean_ecology_website.entity.User;
import com.tju.ocean_ecology_website.service.UserService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import javax.annotation.Resource;

/**
 * 用户表(User)表控制层
 *
 * @author shaolong
 * @since 2025-04-29 19:34:34
 */
@RestController
@RequestMapping("api")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    /**
     * 分页查询
     *
     * @param queryDTO 查询参数，包含分页信息和筛选条件
     * @return 查询结果
     */
    @GetMapping("/users")
    public ResponseEntity<ApiResponse<PageResult<User>>> queryByPage(UserQueryDTO queryDTO) {
        // 创建用户筛选条件
        User user = new User();
        if (queryDTO.getUsername() != null && !queryDTO.getUsername().isEmpty()) {
            user.setUsername(queryDTO.getUsername());
        }
        if (queryDTO.getRole() != null) {
            user.setRole(queryDTO.getRole());
        }
        if (queryDTO.getStatus() != null) {
            user.setStatus(queryDTO.getStatus());
        }

        // 获取分页参数
        PageRequest pageRequest = queryDTO.toPageRequest();

        // 查询数据
        Page<User> page = this.userService.queryByPage(user, pageRequest);

        // 转换为自定义分页结果
        PageResult<User> pageResult = PageResult.fromPage(page, queryDTO.getPage());

        return ResponseEntity.ok(ApiResponse.success(pageResult));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/users/{id}")
    public ResponseEntity<ApiResponse<User>> queryById(@PathVariable("id") Long id) {
        User user = this.userService.queryById(id);
        return ResponseEntity.ok(ApiResponse.success(user));
    }

    /**
     * 新增数据
     *
     * @param user 实体
     * @return 新增结果
     */
    @PostMapping("/users")
    public ResponseEntity<ApiResponse<User>> add(@RequestBody User user) {
        User result = this.userService.insert(user);
        return ResponseEntity.ok(ApiResponse.success(result, "新增成功"));
    }

    /**
     * 编辑数据
     *
     * @param user 实体
     * @return 编辑结果
     */
    @PostMapping("/users/edit/{id}")
    public ResponseEntity<ApiResponse<User>> edit(@PathVariable("id") Long id, @RequestBody User user) {
        user.setId(id);
        User result = this.userService.update(user);
        return ResponseEntity.ok(ApiResponse.success(result, "更新成功"));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/users/{id}")
    public ResponseEntity<ApiResponse<Boolean>> deleteById(@PathVariable("id") Long id) {
        boolean result = this.userService.deleteById(id);
        return ResponseEntity.ok(ApiResponse.success(result, result ? "删除成功" : "删除失败"));
    }

    /**
     * 更新用户状态
     *
     * @param id 用户ID
     * @param statusUpdateDTO 状态更新DTO
     * @return 更新结果
     */
    @PostMapping("/users/{id}/status")
    public ResponseEntity<ApiResponse<User>> updateStatus(
            @PathVariable("id") Long id,
            @Valid @RequestBody StatusUpdateDTO statusUpdateDTO) {
        // 获取当前用户
        User user = this.userService.queryById(id);
        if (user == null) {
            return ResponseEntity.badRequest().body(ApiResponse.error(400, "用户不存在"));
        }

        // 更新状态
        user.setStatus(statusUpdateDTO.getStatus());
        User result = this.userService.update(user);

        return ResponseEntity.ok(ApiResponse.success(result, "状态更新成功"));
    }
}

